package com.sync.data.job;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.sync.data.ievent.entity.IeventBasePositionEmployee;
import com.sync.data.ievent.entity.MtBudget;
import com.sync.data.ievent.repository.MtBudgetRepository;
import com.sync.data.mce.entity.MceBasePositionEmployee;
import com.sync.data.mce.entity.MdmActivityBudget;
import com.sync.data.mce.repository.MdmActivityBudgetRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.data.RepositoryItemReader;
import org.springframework.batch.item.database.JpaItemWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.PlatformTransactionManager;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;

@Configuration
@EnableBatchProcessing
@RequiredArgsConstructor
public class MtBudgetSyncBatchJob {


    private final JobBuilderFactory jobBuilderFactory;

    private final StepBuilderFactory stepBuilderFactory;

    private final PlatformTransactionManager transactionManagerIevent;

    private final MdmActivityBudgetRepository mdmActivityBudgetRepository;

    private final MtBudgetRepository mtBudgetRepository;

    private final JpaItemWriter<MtBudget> mtBudgetJpaItemWriter;

    // 批处理大小
    private static final int CHUNK_SIZE = 100;

    @Bean
    public Job mtMtBudgetSyncJob() {
        return jobBuilderFactory.get("mtMtBudgetSyncJob")
                .incrementer(new RunIdIncrementer())
                .start(syncMtBudgetSyncBatchJobStep())
                .build();
    }


    public Step syncMtBudgetSyncBatchJobStep() {
        return stepBuilderFactory.get("syncMtBudgetSyncBatchJobStep")
                .<MdmActivityBudget, MtBudget>chunk(CHUNK_SIZE)
                .reader(mdmActivityBudgetEmployeeItemReader())
                .processor(positionEmployeeSyncProcessor())
                .writer(mtBudgetJpaItemWriter)
                .transactionManager(transactionManagerIevent)
                .build();
    }

    public ItemReader<MdmActivityBudget> mdmActivityBudgetEmployeeItemReader() {
        RepositoryItemReader<MdmActivityBudget> reader = new RepositoryItemReader<>();
        reader.setRepository(mdmActivityBudgetRepository);
        reader.setMethodName("findAll");
        reader.setSort(Collections.singletonMap("id", Sort.Direction.ASC));
        reader.setPageSize(CHUNK_SIZE);
        return reader;
    }

    public ItemProcessor<MdmActivityBudget, MtBudget> positionEmployeeSyncProcessor() {
        return mdmActivityBudget -> {
             MtBudget mtBudget = mtBudgetRepository.getByExternalId(mdmActivityBudget.getId());

            if (mtBudget == null) {
                mtBudget = new MtBudget();
            }

            mtBudget.setExternalId(mdmActivityBudget.getId());
            mtBudget.setMeetingLevel(mdmActivityBudget.getMeetingLevel());
            mtBudget.setBudgetCode(mdmActivityBudget.getActivityName());
            mtBudget.setName(mdmActivityBudget.getName());
            mtBudget.setCreateTime(new Date());
            mtBudget.setUpdateTime(new Date());
            mtBudget.setBudgetAmount(BigDecimal.valueOf(999999999));
            mtBudget.setFrozenAmount(BigDecimal.valueOf(0));
            mtBudget.setSettlementAmount(BigDecimal.valueOf(0));
            mtBudget.setBudgetType("split");
            mtBudget.setStartTime(DateUtil.parse("2025-01-01 00:00:00", DatePattern.NORM_DATETIME_PATTERN));
            mtBudget.setEndTime(DateUtil.parse("2125-12-31 23:59:59", DatePattern.NORM_DATETIME_PATTERN));
            mtBudget.setActive(1);
            mtBudget.setIsSupplier(1);
            mtBudget.setBudgetNumber(mdmActivityBudget.getActivityName());

            return mtBudget;
        };
    }

}